class websocket {
  constructor() {
    this.url = 'ws://192.168.8.211:8900/websocket/ALL';
  }

  init() {
    // init 时先关闭
    if (this.net) {
      this.close();
    }

    this.net = new WebSocket(this.url);

    this.net.onopen = () => {
      console.log('open...');
    };

    this.net.onclose = () => {
      console.log('close...');
    };

    this.net.onmessage = () => {
      console.log('new message...');
    };

    this.net.onerror = () => {
      // 错误重连
      this.net = new WebSocket(this.url);
    };
  }

  close() {
    if (this.net) {
      this.net.close();
    }
  }
}

export default websocket;